<?php

class ukeyClassAction extends Action
{
    //ukey开关设置
    public function switchAjax()
    {
        $this->option->setval('ukey-switch', $this->request('switchs'));
        $this->backmsg('', '成功');
    }

    public function getSwitchAjax()
    {
        $data = $this->option->getval('ukey-switch');
        $this->backmsg('', '', $data);
    }

    //绑定ukey
    public function bindAjax()
    {
        $id = $this->request('id');
        $key = $this->request('key');
        $info = m('admin')->getone('ukey=\''.$key.'\'', 'ukey');
        if(!empty($info)) $this->backmsg('uk已绑定');
        m('admin')->update(array('ukey' => $key), 'id='.$id);
        $this->backmsg();
    }

    //解绑ukey
    public function unbindAjax()
    {
        $id = $this->request('id');
        m('admin')->update(array('ukey' => ''), 'id='.$id);
        $this->backmsg();
    }

    //ukey用户列表
    public function listsAjax()
    {
        $page = $this->request('page');
        $limit = $this->request('limit');
        $key = $this->request('key');
        $where = 'status = 1';
        if($key) {
            $where .= ' and (`name`=\''.$key.'\' or `deptname`=\''.$key.'\')';
        }
        $sql 		= 'select id,user,name,sex,mobile,deptname,ranking,ukey from [Q]admin where '.$where.' order by id asc';
        $count 		= $this->db->getall($sql);
        if($page <= 0)$page=1;
        $sql	.= " limit ".($page-1)*$limit.",$limit";
        $rows	 = $this->db->getall($sql);
        $data	= array(
            'totalCount'=> count($count),
            'rows'		=> $rows
        );
        return json_encode($data);
    }

}